public class MyLinkedList {

    static class LinkNode{
        public int val;
        public LinkNode prev;
        public LinkNode next;
        public LinkNode(int val){
            this.val = val;
        }
    }

    public LinkNode head;
    public LinkNode last;//当前链表尾结点

    public void display(){
        LinkNode cur = head;
        while (cur != null){
            System.out.println(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //头插法
    public void addFirst(int data){
        LinkNode node = new LinkNode(data);
        if(head == null){
            head = node;
            last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    //尾插法
    public void addLast(int data){
        LinkNode node = new LinkNode(data);
        if(head == null){
            head = node;
            last = node;
        }else{
            last.next = node;
            node.prev = last;
            last = last.next;
        }
    }

    //任意位置插入，第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        //1.检查index合法性
        if(index < 0 || index > size()){
            throw new IndexNotLegalException("Index位置不合法！");
        }
        if (index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        LinkNode node = new LinkNode(data);
        //2.获取当前的index位置的结点的地址
        LinkNode cur = findIndex(index);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    private LinkNode findIndex(int index){
        LinkNode cur = head;
        while(index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        LinkNode cur = head;
        while (cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        LinkNode cur = head;
        while(cur != null){
            //找到了 开始删除
            if(cur.val == key){
                //处理头部问题
                if (cur == head){
                    head = head.next;
                    if(head != null){
                        //防止只有一个结点
                        head.prev = null;
                    }

                }else{
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        //删除的不是尾巴节点
                        cur.next.prev = cur.prev;
                    }else{
                        //删除的是尾巴结点
                        last = cur.prev;
                    }

                }
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        LinkNode cur = head;
        while(cur != null){
            //找到了 开始删除
            if(cur.val == key){
                //处理头部问题
                if (cur == head){
                    head = head.next;
                    if(head != null){
                        //防止只有一个结点
                        head.prev = null;
                    }

                }else{
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        //删除的不是尾巴节点
                        cur.next.prev = cur.prev;
                    }else{
                        //删除的是尾巴结点
                        last = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    //得到单链表的长度
    public int size(){
        int count = 0;
        LinkNode cur = head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void clear(){
        LinkNode cur = head;
        while(cur != null){
            LinkNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }
}
